package com.maxd.modules.coupon.service.impl;

import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.modules.coupon.entity.CouponAmount;
import com.maxd.modules.coupon.entity.CouponOrderDetail;
import com.maxd.modules.coupon.mapper.CouponAmountMapper;
import com.maxd.modules.coupon.mapper.CouponOrderDetailMapper;
import com.maxd.modules.coupon.service.CouponOrderDetailService;
import com.maxd.modules.coupon.utils.CouponCardUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @Auther: Laban:yubo_010@163.com
 * @Date: 2020/11/14 18:35
 * @Description:
 */
@Service
public class CouponOrderDetailServiceImpl implements CouponOrderDetailService {


    @Autowired
    private CouponOrderDetailMapper couponOrderDetailMapper;

    @Autowired
    private CouponAmountMapper couponAmountMapper;

    CouponCardUtil couponCardUtil = new  CouponCardUtil();

    CouponOrderDetail couponOrderDetail = null;

    /**
     * 分页查询
     * @param couponOrderDetail
     * @param pageable
     * @return
     */
    @Override
    public Result findAll(CouponOrderDetail couponOrderDetail, Pageable pageable) {
        Example<CouponOrderDetail> couponOrderDetailExample = Example.of(couponOrderDetail);
        return ResultUtil.success(couponOrderDetailMapper.findAll(couponOrderDetailExample,pageable));
    }

    @Override
    public void updateCouponOrderDetailById(CouponOrderDetail couponOrderDetail) {
        couponOrderDetailMapper.updateCouponOrderDetailById(couponOrderDetail);
    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        couponOrderDetailMapper.deleteById(id);
    }

    /**
     * 根据ids删除
     * @param ids
     */
    @Override
    public void deleteByIdList(String ids) {
        String []id=ids.split(",");
        for(String i:id){
            couponOrderDetailMapper.deleteById(Long.parseLong(i));
        }
    }

    @Override
    public CouponOrderDetail findCouponOrderDetailByOrderId(String orderId) {
        return couponOrderDetailMapper.findCouponOrderDetailByOrderId(orderId);
    }


    /**
     * 保存淘宝订单明细
     * orderChannel TB JD PDD
     */
    @Override
    public Boolean saveTaobaoCouponOrderDetail(String userId, String orderId, String prodId, String orderChannel,String orderStatus,String prodTitle) {
        Boolean isok = Boolean.FALSE;
        couponOrderDetail = new CouponOrderDetail();
        //元转分
        BigDecimal yuan = new BigDecimal("100");

        //各个渠道价格
        String pddProdCouponPrice = null;
        String jdProdCouponPrice = null;
        String taobaoProdCouponPrice = null;

        //判断用户是否存在 抵扣金额表中，如果不再 则 false / 说明此用户未领取优惠券
        CouponAmount couponAmount = couponAmountMapper.getCouponAmountByUserId(userId);
        if(couponAmount==null){
            return false;
        }

        //用户抵扣金额
        BigDecimal userDeductQuota = new BigDecimal(couponAmount.getUserDeductQuota());

        try {
            //判断渠道订单
            if("TB".equals(orderChannel)){
                taobaoProdCouponPrice = couponCardUtil.getTaobaoProdCouponPrice(prodId);//返回的是元

                BigDecimal bigDecimal = new BigDecimal(taobaoProdCouponPrice);
                BigDecimal newAmount = bigDecimal.multiply(yuan);

                CouponOrderDetail couponOrderDetailByOrderId = findCouponOrderDetailByOrderId(orderId);
                if(couponOrderDetailByOrderId==null){
                    CouponOrderDetail couponOrderDetail =
                            this.couponOrderDetail.saveCouponOrderDetail
                      (new Date(), userId, orderId, prodId, newAmount.toString(),prodTitle);
                    //判断订单状态
                    judgeOrderStatus(couponOrderDetail,userId,orderId,orderStatus,userDeductQuota,newAmount);
                }


            }else if("JD".equals(orderChannel)){
                jdProdCouponPrice = couponCardUtil.getJDProdCouponPrice(prodId);//返回的是元

                BigDecimal bigDecimal = new BigDecimal(jdProdCouponPrice);
                BigDecimal newAmount = bigDecimal.multiply(yuan);

                CouponOrderDetail couponOrderDetailByOrderId = findCouponOrderDetailByOrderId(orderId);
                if(couponOrderDetailByOrderId==null){
                    CouponOrderDetail couponOrderDetail =
                            this.couponOrderDetail.saveCouponOrderDetail(new Date(), userId, orderId, prodId, newAmount.toString(),prodTitle);

                    //判断订单状态
                    judgeOrderStatus(couponOrderDetail,userId,orderId,orderStatus,userDeductQuota,newAmount);
                }

            }else if("PDD".equals(orderChannel)){
                pddProdCouponPrice = couponCardUtil.getPddProdCouponPrice(prodId);//返回的是分

                CouponOrderDetail couponOrderDetailByOrderId = findCouponOrderDetailByOrderId(orderId);
                if(couponOrderDetailByOrderId==null){
                    CouponOrderDetail couponOrderDetail =
                            this.couponOrderDetail.saveCouponOrderDetail(new Date(), userId, orderId, prodId,pddProdCouponPrice,prodTitle);

                    //判断订单状态
                    BigDecimal couponPriceDecimal = new BigDecimal(pddProdCouponPrice);
                    judgeOrderStatus(couponOrderDetail,userId,orderId,orderStatus,userDeductQuota,couponPriceDecimal);
                }
            }else {
                return isok;
            }
        }catch (Exception e){
            return isok;
        }


        return Boolean.TRUE;
    }


    /**
     * 判断订单状态
     * @param orderStatus
     * @param userDeductQuota
     * @param couponPriceDecimal
     */
    void judgeOrderStatus(CouponOrderDetail couponOrderDetail,String userId,String orderId,
                          String orderStatus,BigDecimal userDeductQuota,
                          BigDecimal couponPriceDecimal){

        couponOrderDetailMapper.save(couponOrderDetail);
        //判断订单状态 12 已付款 扣款金额到冻结金额中 已付款状态保存
        if("12".equals(orderStatus)){
            //抵扣金额减去优惠券金额
            CouponAmount couponAmountByUserId = couponAmountMapper.getCouponAmountByUserId(userId);
            CouponAmount couponAmount = new CouponAmount();
            BigDecimal subtract = userDeductQuota.subtract(couponPriceDecimal);
            CouponAmount couponAmount1 = couponAmount.saveCouponAmount(couponAmountByUserId.getCreateTime(),couponAmountByUserId.getUserId(),
                    subtract.toString(), couponAmountByUserId.getUserProvinceAmount(), couponPriceDecimal.toString().trim());
            couponAmountMapper.updateCouponAmountByUserId(couponAmount1);

            //3 已结算 将上次冻结的金额减去 3的状态则判断这张是否存在。在进行逻辑运算
        }else if("3".equals(orderStatus)){
            CouponOrderDetail couponOrderDetailByOrderId = findCouponOrderDetailByOrderId(orderId);
            if(couponOrderDetailByOrderId!=null){
                CouponAmount couponAmount = new CouponAmount();
                CouponAmount couponAmount12 = couponAmountMapper.getCouponAmountByUserId(userId);
                BigDecimal userFrozenAmount = new BigDecimal(couponAmount12.getUserFrozenAmount());
                BigDecimal couponAmount1 = new BigDecimal(couponOrderDetailByOrderId.getCouponAmount());
                BigDecimal newBigDecimal = userFrozenAmount.subtract(couponAmount1);
                CouponAmount couponAmount2 = couponAmount.saveCouponAmount(couponAmount12.getCreateTime(),couponAmount12.getUserId(),
                        couponAmount12.getUserDeductQuota().toString(), couponAmount12.getUserProvinceAmount(),newBigDecimal.toString().trim());
                couponAmountMapper.updateCouponAmountByUserId(couponAmount2);
            }
            //13 已失效 将上次冻结的金额加回 13的状态则判断这张是否存在。在进行逻辑运算
        }else if("13".equals(orderStatus)){
            CouponOrderDetail couponOrderDetailByOrderId = findCouponOrderDetailByOrderId(orderId);
            if(couponOrderDetailByOrderId!=null){
                CouponAmount couponAmount = new CouponAmount();
                CouponAmount couponAmount12 = couponAmountMapper.getCouponAmountByUserId(userId);
                BigDecimal userDeductQuota1 = new BigDecimal(couponAmount12.getUserDeductQuota());
                BigDecimal couponAmount1 = new BigDecimal(couponOrderDetailByOrderId.getCouponAmount());
                BigDecimal newBigDecimal = userDeductQuota1.add(couponAmount1);
                CouponAmount couponAmount2 = couponAmount.saveCouponAmount(couponAmount12.getCreateTime(),couponAmount12.getUserId(),
                        newBigDecimal.toString().trim(), couponAmount12.getUserProvinceAmount(),couponAmount12.getUserFrozenAmount());
                couponAmountMapper.updateCouponAmountByUserId(couponAmount2);

            }
        }
    }
}
